from django.contrib import messages
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods

from server.apps.todo.logic.jobs import queries as job_queries
from server.apps.todo.logic.usecases import create_new_job, edit_job


@require_http_methods(['GET'])
def index(request: HttpRequest) -> HttpResponse:
    """
    Main (or index) view.
    Returns rendered default page to the user.
    """
    jobs = job_queries.list_all()
    return render(request, 'todo/index.html', {'jobs': jobs})


@require_http_methods(['POST'])
def create(request: HttpRequest) -> HttpResponse:
    """Creates new post if the passed data is valid."""
    is_created = create_new_job.run(
        title=request.POST.get('title'),
        description=request.POST.get('description'),
        done=bool(request.POST.get('done')),
    )

    messages.info(request, 'Job created: {0}'.format(is_created))
    return render(request, 'todo/create_job.html')


@require_http_methods(['GET'])
def list_jobs(request: HttpRequest) -> HttpResponse: 
    """Lists all jobs."""
    jobs = job_queries.list_all()
    return render(request, 'todo/list_all_jobs.html', {'jobs': jobs})


@require_http_methods(['GET'])
def create_job(request: HttpRequest) -> HttpResponse:
    """Go to create form."""  
    return render(request, 'todo/create_job.html')


@require_http_methods(['GET'])
def list_one(request: HttpRequest) -> HttpResponse:
    """Go to list form.""" 
    return render(request, 'todo/list_one_job.html')


@require_http_methods(['GET'])
def list_one_job(request: HttpRequest) -> HttpResponse:
    """Lists one job by job id."""
    job_id=request.GET.get('job_id')
    job, msg = job_queries.list_one(job_id)

    messages.info(request, msg)
    return render(request, 'todo/list_one_job.html', {'job': job})


@require_http_methods(['GET'])
def edit(request: HttpRequest) -> HttpResponse:
    """Go to edit form.""" 
    return render(request, 'todo/edit_job.html')


@require_http_methods(['POST'])
def edit_job_by_id(request: HttpRequest) -> HttpResponse:
    """Edit job by job id."""
    job = edit_job.edit(
        job_id=request.POST.get('job_id'),
        title=request.POST.get('title'),
        description=request.POST.get('description'),
        done=bool(request.POST.get('done')),
    )

    messages.info(request, 'Job edited: {0}'.format(job))
    return render(request, 'todo/edit_job.html',  {'job': job})


@require_http_methods(['GET'])
def done(request: HttpRequest) -> HttpResponse:
    """Go to done form.""" 
    return render(request, 'todo/done_job.html')


@require_http_methods(['POST'])
def done_job_by_id(request: HttpRequest) -> HttpResponse:
    """Marks job finished by job id."""
    job = edit_job.done(
        job_id=request.POST.get('job_id')
    )

    messages.info(request, 'Job is done: {0}'.format(job))
    return render(request, 'todo/done_job.html',  {'job': job})


@require_http_methods(['GET'])
def delete(request: HttpRequest) -> HttpResponse:
    """Go to delete form.""" 
    return render(request, 'todo/delete_job.html')


@require_http_methods(['POST'])
def delete_job_by_id(request: HttpRequest) -> HttpResponse:
    """Delete job by job id."""
    job = edit_job.delete(
        job_id=request.POST.get('job_id')
    )

    messages.info(request, 'Job is deleted: {0}'.format(job))
    return render(request, 'todo/delete_job.html',  {'job': job})


@require_http_methods(['GET'])
def find(request: HttpRequest) -> HttpResponse:
    """Go to find form.""" 
    return render(request, 'todo/find_job.html')


@require_http_methods(['GET'])
def find_job(request: HttpRequest) -> HttpResponse:
    """Find job by text in title or description."""
    text=request.GET.get('text')
    jobs = job_queries.find_jobs(text)
    return render(request, 'todo/find_job.html', {'jobs': jobs})
